import React, { useEffect } from 'react';
import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { ConfigProvider } from 'antd';
import zhCN from 'antd/locale/zh_CN';

import Login from './pages/Login';
import Dashboard from './pages/Dashboard';
import MainLayout from './components/Layout/MainLayout';

// 基金研究子系统
import FundList from './modules/FundResearch/FundList';
import CompanyList from './modules/FundResearch/CompanyList';
import ManagerList from './modules/FundResearch/ManagerList';
import FundProfile from './modules/FundResearch/FundProfile';

// 因子管理子系统
import FactorList from './modules/FactorManagement/FactorList';
import FactorTree from './modules/FactorManagement/FactorTree';
import CreateDerivedFactor from './modules/FactorManagement/CreateDerivedFactor';
import StyleFactorManagement from './modules/FactorManagement/StyleFactorManagement';

// 策略管理子系统
import StrategyList from './modules/Strategy/StrategyList';
import StrategyBacktest from './modules/Strategy/StrategyBacktest';
import StrategyRebalance from './modules/Strategy/StrategyRebalance';
import StrategyMonitor from './modules/Strategy/StrategyMonitor';

// 组合产品管理子系统
import PortfolioAudit from './modules/Portfolio/PortfolioAudit';
import CreatePortfolio from './modules/Portfolio/CreatePortfolio';
import PortfolioDetail from './modules/Portfolio/PortfolioDetail';

// 交易管理子系统
import PortfolioRebalance from './modules/Trade/PortfolioRebalance';
import ErrorHandling from './modules/Trade/ErrorHandling';
import AccountRebalance from './modules/Trade/AccountRebalance';
import TradeOrderManagement from './modules/Trade/TradeOrderManagement';
import SettlementManagement from './modules/Trade/SettlementManagement';

// 权限验证组件
function PrivateRoute({ children }) {
  // 移除自动登录逻辑，用户必须手动登录
  const token = localStorage.getItem('token');
  const userInfo = localStorage.getItem('userInfo');
  
  // 只有当token和userInfo都存在且有效时才允许访问
  if (token && userInfo) {
    try {
      const parsedUserInfo = JSON.parse(userInfo);
      if (parsedUserInfo.username) {
        return children;
      }
    } catch (e) {
      // 如果userInfo解析失败，清除无效数据
      localStorage.removeItem('token');
      localStorage.removeItem('userInfo');
    }
  }
  
  // 清除可能存在的无效数据
  localStorage.removeItem('token');
  localStorage.removeItem('userInfo');
  
  return <Navigate to="/login" replace />;
}

function App() {
  // 应用启动时清除可能存在的旧登录信息，确保用户必须重新登录
  useEffect(() => {
    // 清除所有登录相关的本地存储
    localStorage.removeItem('token');
    localStorage.removeItem('userInfo');
  }, []);

  return (
    <ConfigProvider locale={zhCN}>
      <BrowserRouter>
        <Routes>
          {/* 登录页面 */}
          <Route path="/login" element={<Login />} />
          
          {/* 主应用路由 */}
          <Route path="/" element={
            <PrivateRoute>
              <MainLayout />
            </PrivateRoute>
          }>
            {/* 默认重定向到仪表板 */}
            <Route index element={<Navigate to="/dashboard" replace />} />
            
            {/* 系统概览 */}
            <Route path="dashboard" element={<Dashboard />} />
            
            {/* 基金研究子系统 */}
            <Route path="fund-research">
              <Route index element={<Navigate to="funds" replace />} />
              <Route path="funds" element={<FundList />} />
              <Route path="companies" element={<CompanyList />} />
              <Route path="managers" element={<ManagerList />} />
              <Route path="profile/:fundId" element={<FundProfile />} />
            </Route>
            
            {/* 因子管理子系统 */}
            <Route path="factor-management">
              <Route index element={<Navigate to="factors" replace />} />
              <Route path="factors" element={<FactorList />} />
              <Route path="tree" element={<FactorTree />} />
              <Route path="create" element={<CreateDerivedFactor />} />
              <Route path="style" element={<StyleFactorManagement />} />
            </Route>
            
            {/* 策略管理子系统 */}
            <Route path="strategy">
              <Route index element={<Navigate to="list" replace />} />
              <Route path="list" element={<StrategyList />} />
              <Route path="backtest" element={<StrategyBacktest />} />
              <Route path="rebalance" element={<StrategyRebalance />} />
              <Route path="monitor" element={<StrategyMonitor />} />
            </Route>
            
            {/* 组合产品管理子系统 */}
            <Route path="portfolio">
              <Route index element={<Navigate to="create" replace />} />
              <Route path="audit" element={<PortfolioAudit />} />
              <Route path="create" element={<CreatePortfolio />} />
              <Route path="detail" element={<PortfolioDetail />} />
            </Route>
            
            {/* 交易管理子系统 */}
            <Route path="trade">
              <Route index element={<Navigate to="order" replace />} />
              <Route path="rebalance" element={<PortfolioRebalance />} />
              <Route path="error" element={<ErrorHandling />} />
              <Route path="account" element={<AccountRebalance />} />
              <Route path="order" element={<TradeOrderManagement />} />
              <Route path="settlement" element={<SettlementManagement />} />
            </Route>
          </Route>
          
          {/* 404 页面 */}
          <Route path="*" element={<Navigate to="/dashboard" replace />} />
        </Routes>
      </BrowserRouter>
    </ConfigProvider>
  );
}

export default App; 